﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Rerun.Exceptions;

namespace Rerun.Util {

    /// <summary>
    /// 
    /// </summary>
    public class ProcessUtil {

        /// <summary>
        /// 
        /// </summary>
        private ProcessUtil() {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="processName"></param>
        /// <returns></returns>
        /// <exception cref="UtilException"></exception>
        public static List<Process> GetProcessesByName(string processName) {
            List<Process> processList;

            try {
                processList = new List<Process>(Process.GetProcessesByName(processName));
            } catch(Exception e) {
                throw new UtilException(string.Format("There was a problem trying to get the list of running processes for the given process name (process name: {0}): {1}", processName, e.Message));
            }

            return processList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        /// <exception cref="UtilException"></exception>
        public static bool StartProcess(Process process) {
            bool started = false;

            try {
                started = process.Start();
            } catch(Exception e) {
                throw new UtilException(string.Format("There was a problem trying to start the process (process file name: {0}): {1}", process.StartInfo.FileName, e.Message));
            }

            return started;
        }
    }
}